//Copyright 2001-2004 Jake Bordens
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.

// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

DefConst('kConvertEntities, func(inText)
	begin
		local outText := Clone(inText);
		StrReplace(outText,"&amp;","&",nil);
		StrReplace(outText,"&gt;", ">", nil);
		StrReplace(outText, "&lt;", "<", nil);
		StrReplace(outText, "&apos;", "'", nil);
		StrReplace(outText, "&quot;", "\"", nil);
		return outText;
	end);

DefConst('kEncodeEntities, func(inText)
	begin
		print(inText);
		local outText := Clone(inText);
		StrReplace(outText,"&","&amp;",nil);
		StrReplace(outText,">", "&gt;", nil);
		StrReplace(outText, "<", "&lt;", nil);
		StrReplace(outText, "'", "&apos;", nil);
		StrReplace(outText, "\"", "&quot;", nil);
		print(outText);
		return outText;
	end);

DefConst('kParseXMLTag, func(XMLtag)
	begin
		if BeginsWith(XMLtag,"<") and EndsWith(XMLtag, ">") then
			begin												
				local unary := nil; //tells if the tag is unary (of form <tag/>
				if Endswith(XMLtag, "/>") then
					begin
					unary := true;
					stripped := Substr(XMLtag, 1, (StrLen(XMLtag)-3));
					end;
				else if (BeginsWith(XMLTag, "<?") and EndsWith(XMLTag, "?>")) then
					stripped := SubStr(XMLtag, 2, StrLen(XMLTag)-4);		
				else			
					stripped := Substr(XMLtag, 1, (StrLen(XMLtag)-2));
								
				
				local outFrame := { _type: 'tag, _unary: unary,};
				
				local tokens := StrTokenize(stripped, $ );
				outFrame._name := call tokens with ();
				while x := call tokens with () do
					begin
						local equalDelim := CharPos(x,$=,0);
						if equalDelim then
							begin
								//there is an equals sign
								local param := SubStr(x,0,equalDelim);
								local value := SubStr(x,equalDelim+1,nil);								
								 
								if (BeginsWith(value, "\"") and not EndsWith(value, "\"")) or 
								   (BeginsWith(value, "'") and not EndsWith(value, "'")) then
									begin
									local endQuote := SubStr(value,0,1);
										
									while not EndsWith(temp := call tokens with (), endQuote) do
										value := value & " " & temp;
									value := value & " " & temp;
									end;
								
								if (BeginsWith(value, "\"") and EndsWith(value, "\""))
									or (BeginsWIth(value, "'") and EndsWIth(value, "'")) then
									value := SubStr(value, 1, StrLen(value)-2);
									
								outFrame.(Intern(param)) := value;
							end;
						else
							outFrame.(Intern(x)) := nil;
					end;
				return outFrame;
			end
		else
			return { _type: 'text, text: XMLtag, };
	end);

